import { reducer } from './reducer';

export const createStore = reducer => {
  let currentState = {};
  let observes = []; // 观察者队列
  function getState() {
    return currentState;
  }
  function dispatch(action) {
    currentState = reducer(currentState, action);
    observes.forEach(fn => fn());
  }
  function subscribe(fn) {
    observes.push(fn);
  }
  dispatch({ type: '@@REDUX_INIT' }); //初始化store数据
  return { getState, dispatch, subscribe };
};

const store = createStore(reducer);
store.subscribe(() => {
  console.log('组件1收到store的通知');
});
store.subscribe(() => {
  console.log('组件2收到store的通知');
});
store.dispatch({ type: 'plus' });
